package com.wm.task.view;

import com.wm.task.dao.BeginDAO;
import com.wm.task.vo.AdminUser;
import com.wm.task.vo.User;

public class MainView extends BaseView {
    private BeginDAO beginDAO = new BeginDAO();
    private User user;
    private AdminUser adminUser;
    private UserView userView;
    private AdminView adminView;

    @Override
    protected void handleChoice(String choice) {
        switch (choice) {
            case "1"://用户注册
                register();
                break;
            case "2"://用户登录
                login();
                break;
            case "3"://找回密码
                resetPassword();
                break;
            case "4"://管理员登录
                adminLogin();
                break;
        }

    }

    private void register() {
        System.out.println("====用户注册====");
        System.out.print("请输入手机号:");
        String phoneNum = sc.next();
        String pass = null;
        while (true) {
            System.out.print("请输入密码:");
            pass = sc.next();
            System.out.print("请再次输入密码:");
            if (pass.equals(sc.next())) {
                break;
            } else {
                System.out.println("两次密码不一致,请重新输入");
            }
        }
        System.out.print("请输入姓名:");
        String name = sc.next();
        System.out.print("请输入密保问题:");
        String secretQuestion = sc.next();
        System.out.print("请输入密保答案:");
        String secretAnswer = sc.next();
        beginDAO.register(phoneNum, pass, name, secretQuestion, secretAnswer);
    }

    private void login() {
        System.out.println("====用户登录====");
        while (true) {
            System.out.print("请输入账号:");
            String phoneNum = sc.next();
            System.out.print("请输入密码:");
            String pass = sc.next();
            user = beginDAO.userLogin(phoneNum, pass);
            if (user != null) {
                System.out.println("登录成功!");
                userView = new UserView(user);
                userView.show();
                return;
            } else {
                System.out.println("账号或密码错误,请重新输入");
            }
        }

    }

    private void resetPassword() {
        System.out.println("====修改密码====");
        while (true) {
            System.out.print("请输入账号(电话):");
            String phoneNum = sc.next();
            user = beginDAO.verPhoneNum(phoneNum);
            if (user == null) {
                System.out.println("该用户不存在");
            } else {
                break;
            }
        }
        System.out.println("密保问题:" + user.getSecretQuestion());
        System.out.print("密保答案:");
        String pass = null;
        if (user.getSecretQuestion().equals(sc.next())) {
            while (true) {
                System.out.print("请输入新密码:");
                pass = sc.next();
                System.out.print("请再次输入密码:");
                if (pass.equals(sc.next())) {
                    break;
                }
            }
        }
        beginDAO.resetPassword(user, pass);
    }

    private void adminLogin() {
        System.out.println("====管理员登录====");
        while (true) {
            System.out.print("请输入管理员账号:");
            String account = sc.next();
            System.out.print("请输入密码:");
            String pass = sc.next();
            adminUser = beginDAO.adminLogin(account, pass);
            if (adminUser != null) {
                System.out.println("登录成功");
                adminView = new AdminView();
                adminView.show();
                return;
            }else{
                System.out.println("账号或密码错误");
            }
        }


    }

    @Override
    protected String getTitle() {
        return "万码影评网V1.0";
    }

    @Override
    protected String[] getMenus() {
        return new String[]{"用户注册", "用户登录", "找回密码", "管理员登录", "退出"};
    }
}
